<?php
/**
* polymerinfo.php
*
* Create on 2016-8-5
* Create by TT
*/
namespace haibao\cms\business;
use haibao\cms\data\imagecollective\ImageCollective;

use \haibao\cms\data\PolymerInfo as PolymerInfoData;

class PolymerInfo extends \haibao\cms\business\Base {
	
	/**
	 * 查询聚合记录
	 * @param unknown_type $objectId
	 * @param unknown_type $objectType
	 */
	public static function getPolymerInfo($objectId,$objectType){
		$data = new PolymerInfoData();
		return $data->getPolymerInfo($objectId, $objectType);
	}
	
	/**
	 * 修改聚合记录
	 * @param int $objectId
	 * @param int $objectType
	 * @param array $arrSet
	 */
	public static function updatePolymarInfo($objectId,$objectType,$arrSet){
		$data = new PolymerInfoData();
		if(empty($arrSet)){
			return;
		}
		$data->updatePolymarInfo($objectId,$objectType,$arrSet);
	}
	
	public static function searchPolymer($keywordsId,$keywordsName,$currentPage,$pageSize,$showTime = null,$isAsc = true,$isPublish = true){
		$pageCount = 1; $resultCount = 0;
		$searchResult = \haibao\cms\business\SphinxDb::searchPolymer($keywordsName, $currentPage, $pageSize,$showTime,$isAsc);
		if ($searchResult && !empty($searchResult['result'])){
			$polymerData = new PolymerInfoData();
			$pageCount = $searchResult['pager']->getPageCount();
			$resultCount = $searchResult['pager']->getResultCount();
		}
		
		$polymerInfoList = self::getPolymerInfoList($searchResult,$currentPage,$keywordsId,$isPublish);
		
		return array('infoList'=>$polymerInfoList,'pageCount'=>$pageCount,'resultCount'=>$resultCount);
	}
	
	public static function getSearchPolymerList($keywordsId,$keywordsName,$currentPage,$pageSize,$showTime = null,$isAsc = true,$isPublish = true){
		$searchResult = \haibao\cms\business\SphinxDb::searchPolymer($keywordsName, $currentPage, $pageSize,$showTime,$isAsc);
		$polymerInfoList = self::getPolymerInfoList($searchResult,$currentPage, $keywordsId,$isPublish);
		return $polymerInfoList;
	}
	
	private static function getPolymerInfoList($searchResult,$currentPage,$keywordsId,$isPublish = true){
		$polymerInfoList = array();
		if ($searchResult && !empty($searchResult['result'])){
			$polymerData = new PolymerInfoData();
			$resultCount = $searchResult['pager']->getResultCount();
			$pageCount = $searchResult['pager']->getPageCount();
			$sphinxDbModel = new \haibao\cms\model\data\SphinxDb();
			$articleIds = $imageIds = $featureIds = $videoIds = array();
			$polymerArcitleModels = $polymerImageModels = $polymerFeatureModels = $polymerVideoModels = array();
			foreach ($searchResult['result'] as $value){
				if ($value['type'] == $sphinxDbModel::TYPE_ARTICLE){
					array_push($articleIds,$value['recordid']);
				}else if ($value['type'] == $sphinxDbModel::TYPE_IMAGE){
					array_push($imageIds,$value['recordid']);
				}else if ($value['type'] == $sphinxDbModel::TYPE_FEATURE){
					array_push($featureIds,$value['recordid']);
				}else if ($value['type'] == $sphinxDbModel::TYPE_VIDEO){
					array_push($videoIds,$value['recordid']);
				}
			}
			if ($articleIds){
				$polymerArcitleModels = self::getArticleModels($articleIds);
			}
			if ($featureIds){
				$polymerFeatureModels = self::getFeatureModels($featureIds);
			}
			if ($videoIds){
				$polymerVideoModels = self::getVideoModels($videoIds);
			}
			if ($imageIds){
				$polymerImageModels = self::getImageModels($imageIds);
			}
			foreach ($searchResult['result'] as $value){
				switch ($value['type']){
					case $sphinxDbModel::TYPE_ARTICLE:
						foreach ($polymerArcitleModels as $model){
							if ($model->ObjectId == $value['recordid']){
								array_push($polymerInfoList,(object)$model->toArray());
								break;
							}
						}
						break;
					case $sphinxDbModel::TYPE_FEATURE:
						foreach ($polymerFeatureModels as $model){
							if ($model->ObjectId == $value['recordid']){
								array_push($polymerInfoList,(object)$model->toArray());
								break;
							}
						}
						break;
					case $sphinxDbModel::TYPE_IMAGE:
						foreach ($polymerImageModels as $model){
							if ($model->ObjectId == $value['recordid']){
								array_push($polymerInfoList,(object)$model->toArray());
								break;
							}
						}
						break;
					case $sphinxDbModel::TYPE_VIDEO:
						foreach ($polymerVideoModels as $model){
							if ($model->ObjectId == $value['recordid']){
								array_push($polymerInfoList,(object)$model->toArray());
								break;
							}
						}
						break;
				}
			}
			$polymerInfoList = array_reverse($polymerInfoList);
			if($polymerInfoList && $isPublish){
				$polymerController = \haibao\cms\business\control\pc\channel\polymer\PolymerController::getPolymerInstance();
				$polymerInfoList = $polymerController->packageNewHtml(array($currentPage=>$polymerInfoList), $keywordsId,true);
			}
		}
		
		return $polymerInfoList;
	}
	
	private static function getArticleModels($articleIds){
		$polymerArcitleModels = array();
		$articleFilter = new \haibao\cms\model\filter\Article();
		$articleFilter->select(array(
				$articleFilter::CONDITION_FIELD_NAME_ID,
				$articleFilter::CONDITION_FIELD_NAME_TITLE,
				$articleFilter::CONDITION_FIELD_NAME_SUMMARY,
				$articleFilter::CONDITION_FIELD_NAME_ARTICLE_PUBLISHID,
				$articleFilter::CONDITION_FIELD_NAME_PUBLISHTIME
		));
		
		$articleTagFilter = new \haibao\cms\model\filter\ArticleTag();
		$articleTagFilter->select(array(
				$articleTagFilter::CONDITION_FIELD_NAME_ARTICLE_ID,
				$articleTagFilter::CONDITION_FIELD_NAME_TAG_TREE
		));
		
		$articleFilter->leftJoin($articleTagFilter,null,$articleFilter::CONDITION_FIELD_NAME_ID,$articleTagFilter::CONDITION_FIELD_NAME_ARTICLE_ID,$articleFilter::CONDITION_FIELD_NAME_ARTICLE_TAG);

		$articleFilter->where($articleFilter::CONDITION_FIELD_NAME_ID, \haibao\frame\data\query\Condition::CONDITION_IN, $articleIds);
		$articleFilter->where($articleFilter::CONDITION_FIELD_NAME_PUBLISHED, \haibao\frame\data\query\Condition::CONDITION_EQUAL, 1);
		
		$data = new \haibao\cms\data\Article();
		$arcitleModels = $data->getAll($articleFilter);
		if($arcitleModels){
			foreach($arcitleModels as $model){
				$polymerInfoModel = new \haibao\cms\model\data\PolymerInfo();
				$polymerInfoModel->ObjectId = $model->Id;
				$polymerInfoModel->Title = $model->Title;
				$polymerInfoModel->Summary = $model->Summary;
				$polymerInfoModel->PublishId = $model->PublishId;
				$polymerInfoModel->PublishTime = $model->PublishTime;
				$polymerInfoModel->ObjectType = \haibao\cms\model\data\PolymerInfo::OBJECT_TYPE_ARTICLE;
				
				$tagListStr = '';
				if($model->ArticleTag){
					foreach($model->ArticleTag as $tag){
						if($tag->TagTree){
							$tagListStr .= '|'.$tag->TagTree;
						}
					}
					$tagListStr = substr($tagListStr, 1);
				}
				$polymerInfoModel->TagList = $tagListStr;
				$polymerArcitleModels[] = $polymerInfoModel;
			}
		}
		return $polymerArcitleModels;
	}
	
	private static function getImageModels($imageIds){
		$polymerImageModels = array();
		$imageFilter = new \haibao\cms\model\filter\imagecollective\ImageCollective();
		$imageFilter->select(array(
				$imageFilter::CONDITION_FIELD_NAME_ID,
				$imageFilter::CONDITION_FIELD_NAME_TITLE,
				$imageFilter::CONDITION_FIELD_NAME_DESCRIPTION,
				$imageFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME,
		));
	
		$imageTagFilter = new \haibao\cms\model\filter\imagecollective\ImageCollectiveTag();
		$imageTagFilter->select(array(
				$imageTagFilter::CONDITION_FIELD_NAME_TAG_COLLECTIVE_ID,
				$imageTagFilter::CONDITION_FIELD_NAME_TAG_TREE
		));
		
		$imageFilter->leftJoin($imageTagFilter,null,$imageFilter::CONDITION_FIELD_NAME_ID,$imageTagFilter::CONDITION_FIELD_NAME_TAG_COLLECTIVE_ID,$imageFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_TAG);
		
		
		$imageFilter->where($imageFilter::CONDITION_FIELD_NAME_ID, \haibao\frame\data\query\Condition::CONDITION_IN, $imageIds);
		$imageFilter->where($imageFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHED, \haibao\frame\data\query\Condition::CONDITION_EQUAL, 1);
	
		$data = new \haibao\cms\data\imagecollective\ImageCollective();
		$imageModels = $data->getAll($imageFilter);
		if($imageModels){
			foreach($imageModels as $model){
				$polymerInfoModel = new \haibao\cms\model\data\PolymerInfo();
				$polymerInfoModel->ObjectId = $model->Id;
				$polymerInfoModel->Title = $model->Title;
				$polymerInfoModel->Summary = $model->Description;
				$polymerInfoModel->PublishId = $model->Id;
				$polymerInfoModel->PublishTime = $model->PublishTime;
				$polymerInfoModel->ObjectType = \haibao\cms\model\data\PolymerInfo::OBJECT_TYPE_IMAGE;
				$tagListStr = '';
				if($model->ImageCollectiveTag){
					foreach($model->ImageCollectiveTag as $tag){
						if($tag->TagTree){
							$tagListStr .= '|'.$tag->TagTree;
						}
					}
					$tagListStr = substr($tagListStr, 1);
				}
				$polymerInfoModel->TagList = $tagListStr;
				$polymerImageModels[] = $polymerInfoModel;
			}
		}
		return $polymerImageModels;
	}
	
	private static function getFeatureModels($featureIds){
		$polymerFeatureModels = array();
		$featureFilter = new \haibao\cms\model\filter\FeatureDefine();
		$featureFilter->select(array(
				$featureFilter::CONDITION_FIELD_NAME_ID,
				$featureFilter::CONDITION_FIELD_NAME_FEATURE_NAME,
				$featureFilter::CONDITION_FIELD_NAME_DESCRIPTION,
				$featureFilter::CONDITION_FIELD_NAME_UPDATE_TIME,
		));
		
		$featureTagFilter = new \haibao\cms\model\filter\FeatureTag();
		$featureTagFilter->select(array(
				$featureTagFilter::CONDITION_FIELD_NAME_FEATURE_ID,
				$featureTagFilter::CONDITION_FIELD_NAME_TAG_ID
		));
		$featureFilter->leftJoin($featureTagFilter,null,$featureFilter::CONDITION_FIELD_NAME_ID,$featureTagFilter::CONDITION_FIELD_NAME_FEATURE_ID,$featureFilter::CONDITION_FIELD_NAME_TAGS);
	
		$featureFilter->where($featureFilter::CONDITION_FIELD_NAME_ID, \haibao\frame\data\query\Condition::CONDITION_IN, $featureIds);
	
		$data = new \haibao\cms\data\imagecollective\ImageCollective();
		$featureModels = $data->getAll($featureFilter);
		if($featureModels){
			foreach($featureModels as $model){
				$polymerInfoModel = new \haibao\cms\model\data\PolymerInfo();
				$polymerInfoModel->ObjectId = $model->Id;
				$polymerInfoModel->Title = $model->FeatureName;
				$polymerInfoModel->Summary = $model->Description;
				$polymerInfoModel->PublishId = $model->Id;
				$polymerInfoModel->PublishTime = $model->UpdateTime;
				$polymerInfoModel->ObjectType = \haibao\cms\model\data\PolymerInfo::OBJECT_TYPE_FEATURE;
				$tagListStr = '';
				if($model->Tags){
					foreach($model->Tags as $tag){
						if($tag->TagId){
							$tagListStr .= '|'.$tag->TagId;
						}
					}
					$tagListStr = substr($tagListStr, 1);
				}
				$polymerInfoModel->TagList = $tagListStr;
				$polymerFeatureModels[] = $polymerInfoModel;
			}
		}
		return $polymerFeatureModels;
	}
	
	private static function getVideoModels($videoIds){
		$polymerVideoModels = array();
		$videoFilter = new \haibao\cms\model\filter\Video();
		$videoFilter->select(array(
				$videoFilter::CONDITION_FIELD_NAME_ID,
				$videoFilter::CONDITION_FIELD_NAME_TITLE,
				$videoFilter::CONDOTION_FIELD_NAME_DESCRIPTION,
				$videoFilter::CONDITION_FIELD_NAME_LASTMODIFYTIME,
		));
		
		$videoTagFilter = new \haibao\cms\model\filter\VideoRelTag();
		$videoTagFilter->select(array(
				$videoTagFilter::CONDITION_FIELD_NAME_VIDEOID,
				$videoTagFilter::CONDITION_FIELD_NAME_TAGID
		));
		$videoFilter->leftJoin($videoTagFilter,null,$videoFilter::CONDITION_FIELD_NAME_ID,$videoTagFilter::CONDITION_FIELD_NAME_VIDEOID,$videoFilter::CONDITION_FIELD_NAME_TAGS);
	
		$videoFilter->where($videoFilter::CONDITION_FIELD_NAME_ID, \haibao\frame\data\query\Condition::CONDITION_IN, $videoIds);
	
		$data = new \haibao\cms\data\imagecollective\ImageCollective();
		$videoModels = $data->getAll($videoFilter);
		if($videoModels){
			foreach($videoModels as $model){
				$polymerInfoModel = new \haibao\cms\model\data\PolymerInfo();
				$polymerInfoModel->ObjectId = $model->Id;
				$polymerInfoModel->Title = $model->Title;
				$polymerInfoModel->Summary = $model->Description;
				$polymerInfoModel->PublishId = $model->Id;
				$polymerInfoModel->PublishTime = $model->LastModifyTime;
				$polymerInfoModel->ObjectType = \haibao\cms\model\data\PolymerInfo::OBJECT_TYPE_VIDEO;
				$tagListStr = '';
				if($model->Tags){
					foreach($model->Tags as $tag){
						if($tag->TagId){
							$tagListStr .= '|'.$tag->TagId;
						}
					}
					$tagListStr = substr($tagListStr, 1);
				}
				$polymerInfoModel->TagList = $tagListStr;
				$polymerVideoModels[] = $polymerInfoModel;
			}
		}
		return $polymerVideoModels;
	}
	
}